import { api } from '../index'

// 添加老人信息
export function addElderly(elderlyData) {
  return api.post('/elderly', elderlyData)
}

// 查询所有老人信息
export function getAllElderly(page = 1, pageSize = 10) {
  return api.get('/elderly', {
    params: { page, pageSize }
  }).then(response => {
    // 转换数据格式
    if (response.data?.rows) {
      response.data.list = response.data.rows.map(item => ({
        id: item.id.toString(),
        name: item.name,
        age: item.age ? parseInt(item.age) : 0,
        sex: item.sex === '男' ? '男' : '女',
        phone: item.phone || '',
        address: item.address || '',
        emergencyContact: {
          name: item.emerName || '',
          phone: item.emerPhone || '',
          relationship: item.emerRelation || ''
        },
        medicalHistory: [],
        allergies: [],
        createdAt: '',
        updatedAt: ''
      }))
      response.data.total = response.data.total || 0
    }
    return response
  })
}

// 根据id获取单个老人信息
export function getElderlyById(id) {
  return api.get(`/elderly/${id}`)
}
//根据手机号获取老人信息
export function getElderlyByPhone(phone) {
  return api.get(`/elderly/phone/${phone}`)
}
// 更新老人信息
export function updateElderly(id, data) {
  return api.put(`/elderly/${id}`, data)
}

// 删除老人信息
export function deleteElderly(id) {
  return api.delete(`/elderly/${id}`)
}
//绑定老人给用户
export function bindElderlyToUser(userId, elderlyId) {
  return api.put(`/user/bind/${userId}/${elderlyId}`)
}

//查询老人
export function searchElderly(searchForm) {
  return api.post(`/searchElderly`,searchForm)
}